package com.hospital.service.sample.service.impl;

import com.hospital.service.project.service.ProInfoBaseService;
import com.hospital.service.publics.service.PubFilePathService;
import com.hospital.service.sample.entity.SamInfoBaseEntity;
import com.hospital.service.sample.model.param.SamInfoBaseQueryPageParam;
import common.core.constant.Constant;
import common.core.entity.GroupEntity;
import common.core.util.CommonUtil;
import common.core.util.CustomizeRuntimeException;
import common.core.util.ExcelUtil;
import enhance.plus.logic.LogicBaseServiceImpl;
import com.hospital.service.sample.entity.SamInfoBase;
import com.hospital.service.sample.mapper.SamInfoBaseMapper;
import com.hospital.service.sample.service.SamInfoBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 实验样本基本表
*
* @author lfb
* @created 2020-10-10 14:26:39
*/
@SuppressWarnings("all")
@Service
public class SamInfoBaseServiceImpl extends LogicBaseServiceImpl<SamInfoBaseMapper, SamInfoBase> implements SamInfoBaseService{

    @Autowired
    private SamInfoBaseMapper samInfoBaseMapper;

    @Autowired
    private PubFilePathService pubFilePathService;

    @Autowired
    private ProInfoBaseService proInfoBaseService;

    /**
     * 生成样本检索编号
     * @return
     */
    @Override
    public String getSamplesNumber() {
       return proInfoBaseService.getCode("sam_info_base",Constant.SAMPLE_NUMBER_PREFIX,"samples_number");
    }

    /**
     * 增加实验样本信息
     * @param copyBean
     */
    @Transactional
    @Override
    public void add(SamInfoBase copyBean) {
        this.save(copyBean);
        pubFilePathService.fileCreate(copyBean.getSamplesNumber(),copyBean.getGroupNumber());
    }

    /**
     * 实验样本导出
     * @param param
     * @param response
     */
    @Override
    public void samInfoBaseExport(SamInfoBaseQueryPageParam param, HttpServletResponse response) {
        log.debug("找到此条数据");
        if (CommonUtil.isEmpty(param)) {
            throw CustomizeRuntimeException.exception("传入的参数为空");
        }
        ///查找实验样本信息
        List<SamInfoBaseEntity> list = this.samInfoBaseMapper.getData(param.getGroupNumber());
        String title = param.getGroupNumber() + "分组的实验样本信息";
        try {
            ExcelUtil.exportExcel(list, title, title, SamInfoBaseEntity.class, title + ".xls", response);
        } catch (Exception e) {
            throw CustomizeRuntimeException.exception("信息导出失败");
        }
    }
}
